#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
const int N = 1e6 + 5;
const ll INF = 1e18;
int T, n, m, a[N];
ll f[N], maxf, add;

int main() {
	freopen("color.in", "r", stdin);
	freopen("color.out", "w", stdout);
	ios::sync_with_stdio(0);
	cin.tie(0);
	cin >> T;
	while (T--) {
		cin >> n;
		m = 0;
		for (int i = 1; i <= n; ++i) {
			cin >> a[i];
			m = max(m, a[i]);
		}
		maxf = add = 0;
		f[0] = 0;
		for (int i = 1; i <= m; ++i) f[i] = -INF;
		for (int i = 2; i <= n; ++i) {
			ll cur = (a[i] == a[i - 1] ? a[i] : 0);
			f[a[i - 1]] = max(f[a[i - 1]], max(maxf, f[a[i]] + a[i]) - cur);
			maxf = max(maxf, f[a[i - 1]]);
			add += cur;
		}
		cout << maxf + add << '\n';
	}
	return 0;
}
